package com.zkca.xf.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zkca.xf.ui.activity.technicalprotect.ElectricityF_LActivity;

import java.util.UUID;

import io.reactivex.functions.Consumer;

/**
 * Created by li on 2019/1/3.
 */

public class DeviceUtils {
    private static String[] permissions = {Manifest.permission.READ_PHONE_STATE};
    static String imei;

    public static String getDeviceId(final Context context) {
        RxPermissions rxPermissions = new RxPermissions((Activity)context);
        rxPermissions.request(permissions)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean granted) throws Exception {
                        //同意授权
                        if (granted) {
                            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                                ToastUtils.show(context, "请添加权限");
                                return;
                            }
                            try {
                                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                                imei = telephonyManager.getDeviceId();
                            } catch (Exception e) {
                                imei = "";
                            }
                        } else {//拒绝
                            ToastUtils.show(context, "请添加权限");
                            return;
                        }
                    }
                });
        return imei;

    }
}
